<?php
namespace MyAdmin\Controller;

use Think\Controller;

class AdvertsController extends CommonController
{
  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }

  //广告模块
  public function index()
  {
    $adverts = M('adverts')->where("app_id={$this->app_id}")->order("types desc, orderby desc, id desc")->select();
    foreach ($adverts as $key => &$value) {
      $value['created_at'] = local_time($value['created_at']);
      $value['updated_at'] = local_time($value['updated_at']);

      switch ($value['status']) {
      case 1:
        $value['status_str'] = "开启";
        break;
      case 2:
        $value['status_str'] = "关闭";
        break;
      default:
        $value['status_str'] = "未知";
        break;
      }

      switch ($value['types']) {
        case 1:
            $value['type_str'] = "店铺广告";
            break;
        case 2:
            $value['type_str'] = "商品广告";
            break;
        case 3:
              $value['type_str'] = "小店优选";
              break;
        default:
            $value['type_str'] = "未知";
            break;
      }
      switch ($value['is_applet']) {
        case 0:
            $value['applet_str'] = "关闭";
            break;
        case 1:
            $value['applet_str'] = "开启";
            break;
        default:
            $value['applet_str'] = "未知";
            break;
      }
    }

    $this->assign("adverts", $adverts);
    $this->display();
  }

  public function add()
  {
    if (I('product_id')) {
      $product_id = I('product_id');
      $product = M('products')->where("id = $product_id")->field("logo, name, shop_id")->find();
      $advert = array(
        'types' => 2,
        'product_id' =>  $product_id,
        'image' => $product['logo'],
        'title' => $product['name'],
        'shop_id' => $product['shop_id'],
        'orderby' => 99,
        'status' => 1
      );
      $this->assign("advert", $advert);
    } elseif (I('shop_id')) {
      $shop_id = I('shop_id');
      $shop = M('shops')->where("id = $shop_id")->field("logo, name")->find();
      $advert = array(
        'types' => 1,
        'image' => $shop['logo'],
        'title' => $shop['name'],
        'shop_id' => $shop_id,
        'orderby' => 99,
        'status' => 1
      );
      $this->assign("advert", $advert);
    }
    $this->display();
  }

  //TODO: refactor
  public function save()
  {
    $data = I('post.');

    if ($_FILES) {
      $files = file_upload($this->app_id);
      if ($files) {
        $data['image'] = $files[0];
      }
    }

    if($data['is_applet'] == 0){
        if ($data['types'] == 1 && $data['shop_id']) {
          $data['link'] = "/pages/share/share?shop_id={$data['shop_id']}&products_share=true";
        } elseif ($data['types'] == 2 && $data['product_id'] && $data['shop_id']) {
          $data['link'] = "/pages/share/share?shop_id={$data['shop_id']}&product_id={$data['product_id']}&product_share=true";
        } elseif ($data['types'] == 3 && $data['shop_id'] && $data['desc']) {
            $data['link'] = "/pages/share/share?shop_id={$data['shop_id']}&products_share=true";
        } else {
            $this->error('请填入有效参数',U('Adverts/add'));
        } 
    }else{
        if ($data['types'] == 1 || $data['types'] == 2 || ($data['types'] == 3 && $data['desc'])) {
          if(!$data['appid']){
            $this->error('请填入有效参数',U('Adverts/add'));
          }
          $data['shop_id'] = 0;
          $data['product_id'] = 0;
        }
    }
    $data['app_id'] = $this->app_id;
    $data['created_at'] = $data['updated_at'] = $this->now_time;

    $data = M('adverts')->create($data);
    $result = M('adverts')->add($data);

    if ($result) {
      $this->success('操作成功', U('Adverts/index'));
    } else {
      $this->error('操作失败');
    }
  }

  public function edit()
  {
    $id = I('id');
    $advert = M('adverts')->where("id=$id")->find();

    $this->assign("advert", $advert);
    $this->display();
  }

  //TODO: refactor
  public function update()
  {
    $data = I('post.');
    if ($_FILES) {
      $files = file_upload($this->app_id);
      if ($files) {
        $data['image'] = $files[0];
      }
    }

    if($data['is_applet'] == 0){
        if ($data['types'] == 1 && $data['shop_id']) {
          $data['link'] = "/pages/share/share?shop_id={$data['shop_id']}&products_share=true";
        } elseif ($data['types'] == 2 && $data['product_id'] && $data['shop_id']) {
          $data['link'] = "/pages/share/share?shop_id={$data['shop_id']}&product_id={$data['product_id']}&product_share=true";
        } elseif ($data['types'] == 3 && $data['shop_id'] && $data['desc']) {
            $data['link'] = "/pages/share/share?shop_id={$data['shop_id']}&products_share=true";
        } else {
          $this->error('请填入有效参数', $_SERVER["HTTP_REFERER"]);
        } 
    }else{
        if ($data['types'] == 1 || $data['types'] == 2 || ($data['types'] == 3 && $data['desc'])) {
          if(!$data['appid']){
            $this->error('请填入有效参数', $_SERVER["HTTP_REFERER"]);
          }
          $data['shop_id'] = 0;
          $data['product_id'] = 0;
        }
    }

    $data['updated_at'] = $this->now_time;
    $result = M('adverts')->where("id=$data[id]")->save($data);

    if ($result) {
      $this->success('操作成功', U('Adverts/index'));
    } else {
      $this->error('操作失败');
    }
  }

  public function delete()
  {
    $id = I('id');
    $result = M('adverts')->where("id=$id")->delete();

    if ($result) {
      $this->success('操作成功', U('Adverts/index'));
    } else {
      $this->error('操作失败');
    }
  }
}